home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.6 / city.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-29  |  1.3 KB  |  58 lines

  1. #include "city.h"
  2.  
  3. CITY::CITY()
  4. {
  5.  
  6. }
  7.  
  8. void CITY::Init(INTPOINT _size)
  9. {
  10.     m_size = _size;
  11.  
  12.     m_objects.clear();
  13.  
  14.     for(int y=0;y<m_size.y;y++)
  15.         for(int x=0;x<m_size.x;x++)
  16.         {
  17.             //Add tile
  18.             m_objects.push_back(OBJECT(TILE, D3DXVECTOR3(x * TILE_SIZE, 0.0f, y * -TILE_SIZE), D3DXVECTOR3(0.0f, 0.0f, 0.0f), D3DXVECTOR3(1.0f, 1.0f, 1.0f)));
  19.  
  20.             //Add house
  21.             float sca_xz = rand()%100 / 1000.0f - 0.05f;
  22.             float sca_y = rand()%500 / 1000.0f - 0.25f;
  23.             int rotation = rand()%4;
  24.             int house = rand()%2 + 1;
  25.             if(x % 3 == 0 && y % 3 == 0)house = PARK;
  26.  
  27.             m_objects.push_back(OBJECT(house, D3DXVECTOR3(x * TILE_SIZE, 0.0f, y * -TILE_SIZE), 
  28.                                      D3DXVECTOR3(0.0f, (D3DX_PI / 2.0f) * rotation, 0.0f), 
  29.                                      D3DXVECTOR3(1.0f + sca_xz, 1.0f + sca_y, 1.0f + sca_xz)));
  30.         }
  31. }
  32.  
  33. void CITY::Render(CAMERA *cam)
  34. {
  35.     for(int i=0;i<m_objects.size();i++)
  36.     {
  37.         if(cam == NULL)
  38.         {
  39.             if(m_objects[i].m_rendered)
  40.                 m_objects[i].Render();
  41.         }
  42.         //else if(cam->Cull(m_objects[i].m_bBox))            //Box culling
  43.         else if(cam->Cull(m_objects[i].m_bSphere))    //Sphere culling
  44.         {
  45.             m_objects[i].m_rendered = false;
  46.         }
  47.         else
  48.         {
  49.             m_objects[i].Render();
  50.             m_objects[i].m_rendered = true;
  51.         }
  52.     }
  53. }
  54.  
  55. D3DXVECTOR3 CITY::GetCenter()
  56. {
  57.     return D3DXVECTOR3(m_size.x / 2.0f * TILE_SIZE, 0.0f, m_size.y / 2.0f * -TILE_SIZE);
  58. }